<template>
  <div class="page-container">
    <div class="content-wrapper">
      <div class="grid-container">
        <div v-for="consultant in consultants" 
             :key="consultant.id" 
             class="grid-item">
          <el-card class="consultant-card">
            <div class="card-header">
              <el-avatar 
                :size="80" 
                :src="consultant.avatar || '/default-avatar.png'"
                class="consultant-avatar" 
              />
              <div class="header-info">
                <h3 class="name">{{ consultant.username }}</h3>
                <span class="title">{{ consultant.title || '心理咨询师' }}</span>
              </div>
            </div>

            <div class="card-body">
              <div class="info-block">
                <h4 class="info-title">专业领域</h4>
                <p class="info-content">{{ consultant.specialty || '暂无' }}</p>
              </div>
              <div class="info-block">
                <h4 class="info-title">个人简介</h4>
                <p class="info-content">{{ consultant.introduction || '暂无介绍' }}</p>
              </div>
            </div>

            <div class="card-footer">
              <el-button 
                type="primary" 
                class="consult-button"
                @click="startConsultation(consultant)"
              >
                开始咨询
              </el-button>
            </div>
          </el-card>
        </div>
      </div>

      <el-empty 
        v-if="!loading && consultants.length === 0" 
        description="暂无可用的咨询师" 
      />

      <div v-if="loading" class="loading-grid">
        <div v-for="i in 6" :key="i" class="grid-item">
          <el-card class="consultant-card">
            <el-skeleton animated>
              <template #template>
                <div class="skeleton-content">
                  <el-skeleton-item variant="circle" style="width: 80px; height: 80px;" />
                  <el-skeleton-item variant="h3" style="width: 150px; margin: 16px 0;" />
                  <el-skeleton-item variant="text" style="width: 100%;" />
                  <el-skeleton-item variant="text" style="width: 100%;" />
                </div>
              </template>
            </el-skeleton>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const router = useRouter()
const consultants = ref([])
const loading = ref(false)

const fetchConsultants = async () => {
  loading.value = true
  try {
    const res = await request.get('/admin/user/experts')
    if (res.data) {
      consultants.value = res.data.records || []
    }
  } catch (error) {
    console.error('获取咨询师列表失败:', error)
    ElMessage.error('获取咨询师列表失败')
  } finally {
    loading.value = false
  }
}

const startConsultation = async (consultant) => {
  try {
    const res = await request.post('/admin/consultation/create', {
      consultantId: consultant.id
    })
    
    if (res.data) {
      router.push({
        path: `/consultation/chat/${res.data.id}`,
        query: {
          consultantId: consultant.id,
          consultantName: consultant.username,
          consultantAvatar: consultant.avatar
        }
      })
    }
  } catch (error) {
    console.error('创建咨询会话失败:', error)
    ElMessage.error('创建咨询会话失败')
  }
}

onMounted(() => {
  fetchConsultants()
})
</script>

<style scoped>
.page-container {
  background-color: #f0f2f5;
  padding: 16px;
  min-height: calc(100% - 32px);  /* 减去上下padding的高度 */
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

/* 移除底部margin */
.grid-container, .loading-grid {
  margin-bottom: 0;
}

.grid-item {
  display: flex;
}

.consultant-card {
  display: flex;
  flex-direction: column;
  width: 100%;
  background: #fff;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.consultant-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.consultant-avatar {
  width: 60px;
  height: 60px;
  flex-shrink: 0;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-info {
  margin-left: 12px;
  flex-grow: 1;
}

.name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #1f2329;
  line-height: 1.4;
}

.title {
  font-size: 13px;
  color: #86909c;
  display: block;
  margin-top: 2px;
}

.card-body {
  flex-grow: 1;
  padding: 12px;
}

.info-block {
  margin-bottom: 12px;
}

.info-block:last-child {
  margin-bottom: 0;
}

.info-title {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 600;
  color: #4e5969;
}

.info-content {
  margin: 0;
  font-size: 13px;
  color: #4e5969;
  line-height: 1.5;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-footer {
  padding: 12px;
  border-top: 1px solid #f0f0f0;
  text-align: center;
}

.consult-button {
  width: 120px;
  height: 32px;
  font-size: 14px;
}

.loading-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.skeleton-content {
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

:deep(.el-card__body) {
  height: 100%;
  padding: 0;
  display: flex;
  flex-direction: column;
}

@media screen and (max-width: 768px) {
  .page-container {
    padding: 12px;
  }

  .grid-container,
  .loading-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}
</style> 